<?php

/*
 * Created by: Dikkehein <Clemens Wijnekus, c.wijnekus@gmail.com>
 */

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Gebruiker extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Gebruiker_model');
        $this->load->database();
    }

    public function index() {
        if ($this->session->userdata('logged_in') == TRUE) {
            $data['query'] = $this->Gebruiker_model->alle_gebruikers();
            $data['gebruiker'] = $this->session->all_userdata();
            $data['title'] = 'Gebruikers overzicht';
            $this->load->view('partial/header', $data);
            $this->load->view('partial/menu', $data);
            $this->load->view('partial/sidebar', $data);
            $this->load->view('gebruiker/index', $data);
        } else {
            redirect('auth');
        }
    }

    public function aanmaken() {
        if ($this->session->userdata('logged_in') == TRUE) {
            // Load support assets
            $this->form_validation->set_error_delimiters('', '<br />');

            // Set validation rules
            $this->form_validation->set_rules('voornaam', 'Voornaam', 'required|min_length[1]|max_length[125]');
            $this->form_validation->set_rules('achternaam', 'Achternaam', 'required|min_length[1]|max_length[125]');
            $this->form_validation->set_rules('email', 'Email', 'required|min_length[1]|max_length[255]|valid_email');
            $this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]|max_length[15]');
            $this->form_validation->set_rules('wachtwoord2', 'Wachtwoord controle', 'required|min_length[5]|max_length[15]|matches[wachtwoord]');

            // Begin validation
            if ($this->form_validation->run() == FALSE) {
                // First load, or problem with form
                $data['voornaam'] = array(
                    'name' => 'voornaam',
                    'id' => 'voornaam',
                    'value' => set_value('voornaam', ''),
                    'maxlength' => '100',
                    'size' => '35');

                $data['achternaam'] = array(
                    'name' => 'achternaam',
                    'id' => 'achternaam',
                    'value' => set_value('achternaam', ''),
                    'maxlength' => '100',
                    'size' => '35');

                $data['email'] = array(
                    'name' => 'email',
                    'id' => 'email',
                    'value' => set_value('email', ''),
                    'maxlength' => '100',
                    'size' => '35');

                $data['wachtwoord'] = array(
                    'name' => 'wachtwoord',
                    'id' => 'wachtwoord',
                    'value' => set_value('wachtwoord', ''),
                    'maxlength' => '100',
                    'size' => '35');

                $data['wachtwoord2'] = array(
                    'name' => 'wachtwoord2',
                    'id' => 'wachtwoord2',
                    'value' => set_value('wachtwoord2', ''),
                    'maxlength' => '100',
                    'size' => '35');

                $data['title'] = 'Gebruiker aanmaken';
                $data['gebruiker'] = $this->session->all_userdata();
                $this->load->view('partial/header', $data);
                $this->load->view('partial/menu', $data);
                $this->load->view('partial/sidebar', $data);
                $this->load->view('gebruiker/aanmaken', $data);
            } else { // Validation passed, now escape the data
                $hash = $this->encrypt->sha1($this->input->post('wachtwoord'));
                $data = array(
                    'voornaam' => $this->input->post('voornaam'),
                    'achternaam' => $this->input->post('achternaam'),
                    'email' => $this->input->post('email'),
                    'wachtwoord' => $hash
                );
                if ($this->Gebruiker_model->aanmaken($data)) {
                    redirect('gebruiker');
                }
            }
        } else {
            redirect('auth');
        }
    }

    public function wijzigen() {
        if ($this->session->userdata('logged_in') == TRUE) {

            // Load support assets
            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('', '<br />');

            // Set validation rules
            $this->form_validation->set_rules('voornaam', 'Voornaam', 'required|min_length[1]|max_length[125]');
            $this->form_validation->set_rules('achternaam', 'Achternaam', 'required|min_length[1]|max_length[125]');
            $this->form_validation->set_rules('email', 'Email', 'required|min_length[1]|max_length[255]|valid_email');
            $this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]|max_length[15]');
            $this->form_validation->set_rules('wachtwoord2', 'Wachtwoord controle', 'required|min_length[5]|max_length[15]|matches[wachtwoord]');

            if ($this->input->post()) {
                $id = $this->input->post('id');
            } else {
                $id = $this->uri->segment(3);
            }

            // Begin validation
            if ($this->form_validation->run() == FALSE) {
                // First load, or problem with form
                $query = $this->Gebruiker_model->gebruiker($id);
                foreach ($query->result() as $row) {
                    $voornaam = $row->voornaam;
                    $achternaam = $row->achternaam;
                    $email = $row->email;
                }

                // First load, or problem with form
                $data['voornaam'] = array(
                    'name' => 'voornaam',
                    'id' => 'voornaam',
                    'value' => set_value('voornaam', $voornaam),
                    'maxlength' => '100',
                    'size' => '35');

                $data['achternaam'] = array(
                    'name' => 'achternaam',
                    'id' => 'achternaam',
                    'value' => set_value('achternaam', $achternaam),
                    'maxlength' => '100',
                    'size' => '35');

                $data['email'] = array(
                    'name' => 'email',
                    'id' => 'email',
                    'value' => set_value('email', $email),
                    'maxlength' => '100',
                    'size' => '35');

                $data['wachtwoord'] = array(
                    'name' => 'wachtwoord',
                    'id' => 'wachtwoord',
                    'value' => set_value('wachtwoord', ''),
                    'maxlength' => '100',
                    'size' => '35');

                $data['wachtwoord2'] = array(
                    'name' => 'wachtwoord2',
                    'id' => 'wachtwoord2',
                    'value' => set_value('wachtwoord2', ''),
                    'maxlength' => '100',
                    'size' => '35');

                $data['id'] = array(
                    'id' => set_value('id', $id)
                );
                $data['gebruiker'] = $this->session->all_userdata();
                $data['title'] = 'Gebruiker wijzigen';
                $this->load->view('partial/header', $data);
                $this->load->view('partial/menu', $data);
                $this->load->view('partial/sidebar', $data);

                $this->load->view('gebruiker/wijzigen', $data);
            } else {
                // Validation passed, now escape the data
                if (empty($this->input->post('wachtwoord')) && empty($this->input->post('wachtwoord2'))) {
                    $data = array(
                        'voornaam' => $this->input->post('voornaam'),
                        'achternaam' => $this->input->post('achternaam'),
                        'email' => $this->input->post('email'),
                    );
                    if ($this->Gebruiker_model->wijzigen($id, $data)) {
                        redirect('gebruiker');
                    }
                } else {
                    $hash = $this->encrypt->sha1($this->input->post('password'));
                    $data = array(
                        'voornaam' => $this->input->post('voornaam'),
                        'achternaam' => $this->input->post('achternaam'),
                        'email' => $this->input->post('email'),
                        'wachtwoord' => $hash,
                    );
                    if ($this->Gebruiker_model->wijzigen($id, $data)) {
                        redirect('gebruiker');
                    }
                }
            }
        } else {
            redirect('auth');
        }
    }

    public function verwijderen() {
        if ($this->session->userdata('logged_in') == TRUE) {

            // Load support assets
            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('', '<br />');
            // Set validation rules
            $this->form_validation->set_rules('id', 'User ID', 'required|min_length[1]|max_length[11]|integer|is_natural');
            if ($this->input->post()) {
                $id = $this->input->post('id');
            } else {
                $id = $this->uri->segment(3);
            }
            if ($this->form_validation->run() == FALSE) {
                // First load, or problem with form
                $data['gebruiker'] = $this->session->all_userdata();
                $data['query'] = $this->Gebruiker_model->gebruiker($id);
                $data['title'] = 'Gebruikers overzicht';
                $this->load->view('partial/header', $data);
                $this->load->view('partial/menu', $data);
                $this->load->view('partial/sidebar', $data);
                $this->load->view('gebruiker/verwijderen', $data);
            } else {
                if ($this->Gebruiker_model->verwijderen($id)) {
                    redirect('gebruiker');
                }
            }
        } else {
            redirect('auth');
        }
    }

}

